home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-08-22 | 4.2 KB | 131 lines | [TEXT/GEOL] |
- Item 4302652 17-Aug-89 17:52
-
- From: D1282 Channelmark, Nancy Melone,PRT
-
- To: MACAPP.TECH$ MACAPP Tech
-
- Sub: DrawShapes Example
-
- Gentlepersons,
-
- I have only recently begun to program in MacApp, and I have already hit
- something I don't quite understand.
-
- In the DrawShapes MacApp example shipped with MacApp 2.0B9, in the routine
- TShapeDocument.DoMakeViews(), it appears that the value of the field fShapeView
- is being used before it is being initialized. The code looks as follows:
-
- PROCEDURE TShapeDocument.DoMakeViews(forPrinting: BOOLEAN);
-
- VAR
- shapeView: TShapeView;
- palette:TPalette;
- aWindow:TWindow;
- aDocState: DocState;
- minSize:Point;
- maxSize:Point;
-
- BEGIN
- IF forPrinting THEN
- palette := NIL
- ELSE
- BEGIN
- New(palette);
- FailNIL(palette);
-
- palette.IPalette(SELF);
- fPaletteView := palette;
- END;
-
- New(shapeView);
- FailNIL(shapeView);
- shapeView.IShapeView(SELF, palette, FALSE);
-
- // notice that shapeView has been initialized, but not assigned to
- // fShapeView
-
- IF NOT forPrinting THEN
- BEGIN
- aWindow := NewPaletteWindow(kShapeWindowRSRCID,
- kWantHScrollBar,
- kWantVScrollBar,
- SELF,
- fShapeView, // NOT a var parameter; it
- // looks like its being used
- // before it's initialized
- fPaletteView,
- kPaletteWidth,
- kLeftPalette);
-
- // if fShapeView is not initialized, this will lead to disaster
- fShapeView.fScroller := fShapeView.GetScroller(TRUE);
-
- // etc.
-
- The comments following C++ style comment delimiters are my own. Doesn't this
- code look suspicious? If you poke around a bit, as I did, you'll find that in
- the routine TShapeView.IShapeView(), which is called in the code fragment
- above, the following code is executed:
-
- PROCEDURE TShapeView.IShapeView(itsDocument: TShapeDocument;
- itsPalette: TPalette;
- forClipboard: BOOLEAN);
-
- VAR
- itsLocation: VPoint;
- itsSize:VPoint;
- aHandler: TStdPrintHandler;
- aDocState: DocState;
- sd: SizeDeterminer;
-
- BEGIN
- fDragging := FALSE;
- fPalette := itsPalette;
- SetVPt(itsSize, kMaxCoord, kMaxCoord);
- IF forClipboard THEN
- sd := sizeVariable
- ELSE
- sd := sizeFillPages;
-
- IView(itsDocument, NIL, gZeroVPt, itsSize, sd, sd);
- fScroller := NIL;
-
- IF itsDocument <> NIL THEN
- itsDocument.fShapeView := SELF;
-
- // etc.
-
- Notice that in the last little IF statement, the given document's fShapeView
- field gets initialized. Since TShapeView.IShapeView() gets executed before
- fShapeView is used in NewPaletteWindow(), everything works.
-
- So, my question is one of object-oriented programming style. In the example as
- described above, the document's fShapeView field is initialized not in the
- document's initialization code, but rather in the code that initializes the
- shape view itself! This is certainly counter to my intuitiveness.
-
- Wouldn't it be more clear to have the TShapeDocument.DoMakeViews() code read:
- New(shapeView);
- FailNIL(shapeView);
- shapeView.IShapeView(SELF, palette, FALSE);
- fShapeView := shapeView;
-
- …and to have the TShapeView.IShapeView() code NOT contain the final IF
- statement listed above, that assigns SELF to fShapeView? None of the routines
- below the IF statement depend on the value of fShapeView.
-
- I am not bringing this up to pick nits. I am just concerned that this odd code
- may be an example of GOOD MacApp code, and that my not understanding this
- example means that I am missing some subtle aspect of OOP, Object Pascal, or
- MacApp.
-
- Thanks for your help!
-
-
- James Plamondon
- Software Engineer
- PowerUp Software
- San Mateo, CA 94403
- (415) 345-5900 x351
-
-